<template>
  <div class="app-container">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline" @submit.native.prevent>
        <el-form-item label="分类名称">
          <el-input v-model="listQuery.q_name" size="small"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="this.getList">查询</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="button-table-margin">
        <el-button size="small" type="primary" @click="add"  v-permission="['/admin/promotion/biddingtype/create']">添加</el-button>
        <el-button size="small" type="primary" @click="edit" v-permission="['/admin/promotion/biddingtype/update']">修改</el-button>
        <el-button size="small" type="primary" @click="del" v-permission="['/admin/promotion/biddingtype/del']">删除</el-button>
        <el-button size="small" type="primary" @click="gridUp" v-permission="['/admin/promotion/biddingtype/auditYes']">启用</el-button>
        <el-button size="small" type="primary" @click="gridDown" v-permission="['/admin/promotion/biddingtype/auditNo']">停用</el-button>
      </div>
      <el-table
        ref="singleTable"
        v-loading="listLoading"
        :data="dataList"
        border
        fit
        highlight-current-row
        @current-change="handleCurrentChange"
        style="width: 100%">
        <el-table-column type="index" align="center" width="50"></el-table-column>
        <el-table-column property="name" width="200" label="分类名称"></el-table-column>
        <el-table-column property="sort" width="200" label="排序"></el-table-column>
        <el-table-column property="state" width="150" label="使用状态">
          <template slot-scope="scope">
            {{ codeBox.USE_YN[scope.row.state] }}
          </template>
        </el-table-column>
        <el-table-column property="createName" width="200" label="添加人"></el-table-column>
        <el-table-column property="createTime" width="200" label="添加时间"></el-table-column>
        <el-table-column property="updateTime" width="200" label="修改更新时间"></el-table-column>
        <el-table-column fixed="right" label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button @click="gridUpRow(scope.row)" type="text" size="small">启用</el-button>
            <el-button @click="gridDownRow(scope.row)" type="text" size="small">停用</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

      <el-dialog title="添加" :visible.sync="dialogFormVisibleAdd">
        <el-form ref="actBiddingType" :rules="rules" :model="actBiddingType" label-position="right" label-width="100px" style="width: 400px; margin-left:50px;">
          <el-form-item label="标题" prop="name">
            <el-input v-model="actBiddingType.name" type="text" />
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="actBiddingType.sort" :min="0" :max="99" :precision="0"></el-input-number>
          </el-form-item>
          <el-form-item>
            <span class="form-word-hint">排序必须填写，输入0到99之间的数字，数字越大顺序越靠前</span>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleAdd = false">
            取消
          </el-button>
          <el-button type="primary" @click="createSubmit()">
            提交
          </el-button>
        </div>
      </el-dialog>

      <el-dialog title="修改" :visible.sync="dialogFormVisibleEdit">
        <el-form ref="actBiddingType" :rules="rules" :model="actBiddingType" label-position="right" label-width="100px" style="width: 400px; margin-left:50px;">
          <el-form-item label="标题" prop="name">
            <el-input v-model="actBiddingType.name" type="text" />
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="actBiddingType.sort" :min="0" :max="99" :precision="0"></el-input-number>
          </el-form-item>
          <el-form-item>
            <span class="form-word-hint">排序必须填写，输入0到99之间的数字，数字越大顺序越靠前</span>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleEdit = false">
            取消
          </el-button>
          <el-button type="primary" @click="updateSubmit()">
            提交
          </el-button>
        </div>
      </el-dialog>

  </div>
</template>

<script>
import { listActBiddingType,createActBiddingType,updateActBiddingType,
    delActBiddingType,auditYesActBiddingType,auditNoActBiddingType} from '@/api/promotion'
import Pagination from '@/components/Pagination'
import { Message } from 'element-ui'
import { getCodeManager,codeToArray} from '@/api/all'
export default {
  name: 'grouptypelist',
  components: { Pagination },
  data() {
    return {
      currentRow: null,
      dataList: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        q_name: ''
      },
      dialogFormVisibleAdd: false,
      dialogFormVisibleEdit: false,
      actBiddingType: {
        id: '',
        name: '',
        sort: 0
      },
      rules: {
        name: [{ required: true, message: '请输入分类标题', trigger: 'blur' },
            { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }],
        sort: [{ required: true, message: '排序不能为空', trigger: 'change' }],
      }
    }
  },
  created() {
    getCodeManager('USE_YN,YES_NO', () => {
    })
    this.getList()
  },
  methods:{
    handleCurrentChange(val) {
      // 选中当前行数据
      this.currentRow = val
    },
    // 获取列表数据
    getList() {
      this.listLoading = true
      listActBiddingType(this.listQuery).then(response => {
        this.dataList = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    reset() {
      this.listQuery = {
        page: 1,
        limit: 20,
        q_name: ''
      }
      this.getList()
    },
    add() {
      this.actBiddingType = {
        id: '',
        name: '',
        sort: 0
      }
      this.dialogFormVisibleAdd = true
      this.$nextTick(() => {
        this.$refs['actBiddingType'].clearValidate()
      })
    },
    createSubmit() {
      this.$refs['actBiddingType'].validate((valid) => {
        if (valid) {
          createActBiddingType(this.actBiddingType).then(res => {
            if(!res.data.success) {
              Message.error(res.data.message);
              return;
            }
            this.dialogFormVisibleAdd = false
            this.getList()
            Message.success('操作成功')
          })
        }
      })
    },
    edit() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.state == 1) {
        Message.error('先停止使用，再进行修改')
        return
      }
      this.actBiddingType = {
        id: this.currentRow.id,
        name: this.currentRow.name,
        sort: this.currentRow.sort
      }
      this.dialogFormVisibleEdit = true
      this.$nextTick(() => {
        this.$refs['actBiddingType'].clearValidate()
      })
    },
    updateSubmit() {
      this.$refs['actBiddingType'].validate((valid) => {
        if (valid) {
          updateActBiddingType(this.actBiddingType).then(res => {
            if(!res.data.success) {
              Message.error(res.data.message);
              return;
            }
            this.dialogFormVisibleEdit = false
            this.getList()
            Message.success('操作成功')
          })
        }
      })
    },
    del() {
      if(this.currentRow.length == 0) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.state == 1) {
        Message.error('使用中不能删除')
        return
      }
      this.$confirm('确定要删除吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delActBiddingType({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    gridUp() {
      if(this.currentRow.length == 0) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.state == 1) {
        Message.error('已经是启用状态，请勿重复操作')
        return
      }
      this.$confirm('确定启用吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        auditYesActBiddingType({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    gridDown() {
      if(this.currentRow.length == 0) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.state == 0) {
        Message.error('已经是停用状态，请勿重复操作')
        return
      }
      this.$confirm('确定停用吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        auditNoActBiddingType({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    gridUpRow(currentRowSelect) {
      if(currentRowSelect.state == 1) {
        Message.error('已经是启用状态，请勿重复操作')
        return
      }
      this.$confirm('确定启用吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        auditYesActBiddingType({id: currentRowSelect.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    gridDownRow(currentRowSelect) {
      if(currentRowSelect.state == 0) {
        Message.error('已经是停用状态，请勿重复操作')
        return
      }
      this.$confirm('确定停用吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        auditNoActBiddingType({id: currentRowSelect.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    }

  }
}
</script>

<style>
</style>
